﻿Imports ControllerSigeco

Public Class frmMercaderiaLoteAjuste

    Dim classController As New controller_stock

    Private _idMercaderiaLote As Integer = 0

    Public Property idMercaderiaLote() As Integer
        Get
            Return _idMercaderiaLote
        End Get
        Set(ByVal value As Integer)
            _idMercaderiaLote = value
        End Set
    End Property

    Private _soloLectura As Boolean = False

    Public Property soloLectura() As Boolean
        Get
            Return _soloLectura
        End Get
        Set(ByVal value As Boolean)
            _soloLectura = value
        End Set
    End Property

    Private Sub frmMercaderia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CargarCombos()
        CargarMercaderias()
    End Sub

    Private Sub CargarCombos()
        Dim dsMarca As New DataTable
        dsMarca = classController.get_marcas(oiml.dbConn, 0, "")
        If Not dsMarca Is Nothing Then
            cboMarca.DataSource = dsMarca
            cboMarca.DisplayMember = "marca"
            cboMarca.ValueMember = "idMarca"
        End If

        Dim dsRubro As New DataTable
        dsRubro = classController.get_rubros(oiml.dbConn, 0, "")
        If Not dsRubro Is Nothing Then
            cboRubro.DataSource = dsRubro
            cboRubro.DisplayMember = "rubro"
            cboRubro.ValueMember = "idRubro"
        End If

        'Dim dsImpuesto As New DataTable
        'dsImpuesto = classController.get_impuestos(oiml.dbConn, 0, "")
        'If Not dsImpuesto Is Nothing Then
        '    cboImpuesto.DataSource = dsImpuesto
        '    cboImpuesto.DisplayMember = "impuesto"
        '    cboImpuesto.ValueMember = "idImpuesto"
        'End If

    End Sub

    Private Sub Control_soloLectura(ByVal value As Boolean)
        txtMercaderia.ReadOnly = value
        txtCodigoBarra.ReadOnly = value
        'txtStockMinimo.ReadOnly = value
        cboMarca.Enabled = Not value
        cboRubro.Enabled = Not value
        'txtObs.ReadOnly = value
        'cboImpuesto.Enabled = Not value
        'btnEliminar.Enabled = Not value
        'btnGrabar.Enabled = Not value
    End Sub

    Private Sub CargarMercaderias()
        Try
            If idMercaderiaLote > 0 Then

                Dim ds As New DataTable
                ds = classController.get_mercaderias_lotes_All(oiml.dbConn, 0, "", 0, 0, False, "", 0, idMercaderiaLote)

                If Not ds Is Nothing Then
                    txtMercaderia.Text = ds.Rows(0)("mercaderia").ToString()
                    txtCodigoBarra.Text = ds.Rows(0)("codigoBarra").ToString()
                    txtCantidad.Text = ds.Rows(0)("cantidad").ToString()
                    cboMarca.SelectedValue = ds.Rows(0)("idMarca").ToString()
                    cboRubro.SelectedValue = ds.Rows(0)("idRubro").ToString()
                    txtSaldo.Text = ds.Rows(0)("saldo").ToString()
                    'cboImpuesto.SelectedValue = ds.Rows(0)("idImpuesto").ToString()
                    txtActualizadoPor.Text = ds.Rows(0)("usuario").ToString()
                    txtEnFecha.Text = ds.Rows(0)("fechaBaja_").ToString()
                    btnEliminar.Enabled = True

                    If soloLectura Then
                        Control_soloLectura(True)
                    End If

                Else

                End If

                txtMercaderia.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtMercaderia.Text = "" Then
            ErrorProvider1.SetError(txtMercaderia, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtMercaderia, "")
        End If        '

        If txtSaldo.Text = "" Or Not IsNumeric(txtSaldo.Text) Then
            ErrorProvider1.SetError(txtSaldo, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtSaldo, "")
        End If        '

        If Not IsNumeric(cboMarca.SelectedValue) Then
            ErrorProvider1.SetError(cboMarca, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(cboMarca, "")
        End If        '

        If Not IsNumeric(cboRubro.SelectedValue) Then
            ErrorProvider1.SetError(cboRubro, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(cboRubro, "")
        End If        '

        'If Not IsNumeric(cboImpuesto.SelectedValue) Then
        '    ErrorProvider1.SetError(cboImpuesto, "Campo obligatorio")
        '    controlCampos = True
        'Else
        '    ErrorProvider1.SetError(cboImpuesto, "")
        'End If        '

        Return controlCampos

    End Function

    Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If idMercaderiaLote > 0 Then
            If classController.Update_mercaderia_lote(oiml.dbConn, idMercaderiaLote, txtSaldo.Text, oiml.idUsuario) Then
                MessageBox.Show("Registro grabado con exito!", "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If
        End If

        

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click

        If idMercaderiaLote > 0 Then
            If MessageBox.Show("Desea dar de baja el registro seleccionado?", "Mercaderias", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Baja_mercaderia(oiml.dbConn, idMercaderiaLote, oiml.idUsuario) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

End Class